USE_MP3 = TRUE
USE_FAT = TRUE

.SUFFIXES:

ifeq ($(strip $(DEVKITPPC)),)
	$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=<path to>devkitPPC")
endif

ifeq ($(strip $(MLPATH)),)
	$(error "Please set MLPATH in your environment. export MLPATH=<path to>MLlib)
endif
 
include $(DEVKITPPC)/wii_rules
 
TARGET		:=	$(notdir $(CURDIR))
BUILD		:=	build
SOURCES		:=	source
DATA		:=	data  
INCLUDES	:=
 
CFLAGS		=	-g -O2 -mrvl -Wall $(MACHDEP) $(INCLUDE) -I$(DEVKITPPC)/local/include
CXXFLAGS	=	$(CFLAGS)
LDFLAGS		=	-g $(MACHDEP) -mrvl -Wl,-Map,$(notdir $@).map
 
LIBS	:=	-lMLlib -lfreetypegx -lfreetype -lMetaphrasis -lpng -lz -lwiiuse -lbte -lfat -lasnd -lmad -logc -lm 
 
LIBDIRS	:=	$(MLPATH)
 
ifneq ($(BUILD), $(notdir $(CURDIR)))
 
export OUTPUT	:=	$(CURDIR)/$(TARGET)
 
export VPATH	:=	$(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
					$(foreach dir,$(DATA),$(CURDIR)/$(dir))
 
export DEPSDIR	:=	$(CURDIR)/$(BUILD)
 
CFILES		:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES	:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
sFILES		:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
SFILES		:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S)))
BINFILES	:=	$(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
 
ifeq ($(strip $(CPPFILES)),)
	export LD	:=	$(CC)
else
	export LD	:=	$(CXX)
endif
 
export OFILES	:=	$(addsuffix .o,$(BINFILES)) \
					$(CPPFILES:.cpp=.o) $(CFILES:.c=.o) \
					$(sFILES:.s=.o) $(SFILES:.S=.o)
 
export INCLUDE	:=	$(foreach dir,$(INCLUDES), -iquote $(CURDIR)/$(dir)) \
					$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
					-I$(CURDIR)/$(BUILD) \
					-I$(LIBOGC_INC)
 
export LIBPATHS	:=	$(foreach dir,$(LIBDIRS),-L$(dir)/lib) \
					-L$(LIBOGC_LIB) -L$(DEVKITPPC)/local/lib
 
export OUTPUT	:=	$(CURDIR)/$(TARGET)

.PHONY: $(BUILD) clean
 
$(BUILD):
	@[ -d $@ ] || mkdir -p $@
	@make --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
 
clean:
	@echo clean ...
	@rm -fr $(BUILD) $(OUTPUT).elf $(OUTPUT).dol
 
run:
	wiiload $(TARGET).dol
 
reload:
	wiiload -r $(TARGET).dol
 
else
 
DEPENDS	:=	$(OFILES:.o=.d)
 
$(OUTPUT).dol: $(OUTPUT).elf
$(OUTPUT).elf: $(OFILES)
 
%.jpg.o	:	%.jpg

	@echo $(notdir $<)
	$(bin2o)
	
%.bin.o	:	%.bin

	@echo $(notdir $<)
	@$(bin2o)
	
%.bmp.o	:	%.bmp

	@echo $(notdir $<)
	@$(bin2o)

%.mp3.o	:	%.mp3

	@echo $(notdir $<)
	@$(bin2o)
	
%.mod.o	:	%.mod

	@echo $(notdir $<)
	@$(bin2o)	
	
%.png.o	:	%.png

	@echo $(notdir $<)
	@$(bin2o)

%.ttf.o	:	%.ttf

	@echo $(notdir $<)
	@$(bin2o)
 
-include $(DEPENDS)
 
endif
